package com.turing.post.organization.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.organization.util.VoucherAdjustOrder;
import com.turing.post.organization.util.VoucherAdjustOrderDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author wzd
 * @2013-03-12
 */
@Stateless(mappedName = "organization.impl.VoucherAdjustOrderDaoImpl")
@Remote
@Local
public class VoucherAdjustOrderDaoImpl implements VoucherAdjustOrderDao {

	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	/**
	 * 根据条件查询凭证调整单列表 --调整单查询
	 * 
	 * @param BranchNo
	 *            调入机构号
	 * @param status
	 *            状态
	 * @param tranType
	 *            交易类型
	 * @param startTranTime
	 *            起始时间
	 * @param endTranTime
	 *            终止时间
	 * @param page
	 *            页码
	 * @return 凭证调整单列表
	 */
	@SuppressWarnings("unchecked")
	public List<VoucherAdjustOrder> getVoucherAdjustOrderList(
			PCPSWebInput webInput, String BranchNo, String status,
			String TranType, String startTranTime, String endTranTime,
			Integer page) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo())
				|| PCPSUtil.isEmpty(startTranTime)
				|| PCPSUtil.isEmpty(endTranTime) || PCPSUtil.isEmpty(BranchNo))
			return null;
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String issuecompanyno = PCPSUtil.getIssueCompanyNo(webInput);
		String sql = "select * from VoucherAdjustOrder where IssueCompanyNo='"
				+ issuecompanyno + "' and TranTime >=to_date('" + startTranTime
				+ "','yyyy-mm-dd hh24:mi:ss') and TranTime <=to_date('"
				+ endTranTime + "','yyyy-mm-dd hh24:mi:ss')";

		sql += PCPSUtil.getSqlCond(webInput, BranchNo, "BranchNo");

		if (!PCPSUtil.isEmpty(status)) {
			sql = sql + " and status = '" + status + "'";
		}
		if (!PCPSUtil.isEmpty(TranType)) {
			sql = sql + " and TranType = '" + TranType + "'";
		}
		sql = sql + " order by TranTime desc";
		Query query=null;
		if(page==-1)
		   query = _em.createNativeQuery(sql, VoucherAdjustOrder.class);
		else
		   query = _em.createNativeQuery(sql, VoucherAdjustOrder.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		return query.getResultList();
	}

	/**
	 * @param BranchNo
	 *            --调整单查询 调入机构号
	 * @param status
	 *            状态
	 * @param tranType
	 *            交易类型
	 * @param startTranTime
	 *            起始时间
	 * @param endTranTime
	 *            终止时间
	 * @return 凭证调整单录数
	 */
	@Override
	public BigInteger getVoucherAdjustOrderListTotal(PCPSWebInput webInput,
			String BranchNo, String status, String TranType,
			String startTranTime, String endTranTime) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo())
				|| PCPSUtil.isEmpty(startTranTime)
				|| PCPSUtil.isEmpty(endTranTime) || PCPSUtil.isEmpty(BranchNo))
			return null;
		String issuecompanyno = PCPSUtil.getIssueCompanyNo(webInput);
		String sql = "select nvl(count(*),0) from VoucherAdjustOrder where "
				+ "IssueCompanyNo='" + issuecompanyno + "' "
				+ "and TranTime >=to_date('" + startTranTime
				+ "','yyyy-mm-dd hh24:mi:ss') " + "and TranTime <=to_date('"
				+ endTranTime + "','yyyy-mm-dd hh24:mi:ss') ";

		sql += PCPSUtil.getSqlCond(webInput, BranchNo, "BranchNo");

		if (!PCPSUtil.isEmpty(status)) {
			sql = sql + " and status = '" + status + "'";
		}
		if (!PCPSUtil.isEmpty(TranType)) {
			sql = sql + " and TranType = '" + TranType + "'";
		}
		Query query = _em.createNativeQuery(sql);
		return new BigInteger(query.getSingleResult().toString());
	}

	/**
	 * 新增凭证调整单
	 * 
	 * @param VoucherAdjustOrder
	 *            凭证调整单
	 * @throws PCPSApplicationException
	 */
	@Override
	public void insertVoucherAdjustOrder(VoucherAdjustOrder voucherAdjustOrder)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, voucherAdjustOrder);
	}

	/**
	 * 修改凭证调整单状态
	 * 
	 * @param VoucherAdjustOrder
	 *            凭证调整单
	 * @throws PCPSApplicationException
	 */
	public void updateVoucherAdjustOrderByStatus(
			VoucherAdjustOrder voucherAdjustOrder)
			throws PCPSApplicationException {
		VoucherAdjustOrder voucherAdjustOrder1 = getVoucherAdjustOrderById(voucherAdjustOrder
				.getVoucherAdjustOrderId());
		if (voucherAdjustOrder1 != null) {
			voucherAdjustOrder1.setStatus(voucherAdjustOrder.getStatus());
			voucherAdjustOrder1.setTranType(voucherAdjustOrder.getTranType());
			voucherAdjustOrder1.setRemark(voucherAdjustOrder.getRemark());
			voucherAdjustOrder1.setAuthOperId(voucherAdjustOrder
					.getAuthOperId());
			voucherAdjustOrder1.setAuthTime(voucherAdjustOrder.getAuthTime());
			if (voucherAdjustOrder.getCancelVoucherAdjustOrderId() != null)
				voucherAdjustOrder1.getCancelVoucherAdjustOrderId();
			GeneralDao.update(_em, voucherAdjustOrder1);
		}
	}

	/**
	 * 根据条件查询凭证库存列表对象
	 * 
	 * @param voucherAdjustOrderId
	 *            调整单ID
	 * @throws PCPSApplicationException
	 */
	public VoucherAdjustOrder getVoucherAdjustOrderById(
			BigInteger voucherAdjustOrderId) {
		return _em.find(VoucherAdjustOrder.class, voucherAdjustOrderId);
	}

	/**
	 * 根据条件查询凭证调整单列表 --调整单申请查询
	 * 
	 * @param BranchNo
	 *            调入机构号
	 * @param status
	 *            状态
	 * @param tranType
	 *            交易类型
	 * @param startTranTime
	 *            起始时间
	 * @param endTranTime
	 *            终止时间
	 * @param page
	 *            页码
	 * @return 凭证调整单列表
	 */
	@SuppressWarnings("unchecked")
	public List<VoucherAdjustOrder> getVoucherAdjustOrderByApplyList(
			PCPSWebInput webInput, String BranchNo, String status,
			String TranType, String startTranTime, String endTranTime,
			Integer page) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo())
				|| PCPSUtil.isEmpty(startTranTime)
				|| PCPSUtil.isEmpty(endTranTime) || PCPSUtil.isEmpty(BranchNo))
			return null;
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String issuecompanyno = PCPSUtil.getIssueCompanyNo(webInput);
		String sql = "select * from VoucherAdjustOrder where IssueCompanyNo='"
				+ issuecompanyno + "' and TranTime >=to_date('" + startTranTime
				+ "','yyyy-mm-dd hh24:mi:ss') and TranTime <=to_date('"
				+ endTranTime + "','yyyy-mm-dd hh24:mi:ss')";

		sql += PCPSUtil.getSqlCond(webInput, BranchNo, "BranchNo");

		if (!PCPSUtil.isEmpty(status)) {
			sql = sql + " and status = '" + status + "'";
		}
		if (!PCPSUtil.isEmpty(TranType)) {
			sql = sql + " and TranType = '" + TranType + "'";
		}
		sql = sql + " and TranType not in('11','12','13')";
		sql = sql + " order by TranTime desc";
		Query query = _em.createNativeQuery(sql, VoucherAdjustOrder.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		return query.getResultList();
	}

	/**
	 * @param BranchNo
	 *            --调整单申请查询 调入机构号
	 * @param status
	 *            状态
	 * @param tranType
	 *            交易类型
	 * @param startTranTime
	 *            起始时间
	 * @param endTranTime
	 *            终止时间
	 * @return 凭证调整单录数
	 */
	@Override
	public BigInteger getVoucherAdjustOrderByApplyListTotal(
			PCPSWebInput webInput, String BranchNo, String status,
			String TranType, String startTranTime, String endTranTime) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo())
				|| PCPSUtil.isEmpty(BranchNo))
			return BigInteger.ZERO;
		String issuecompanyno = PCPSUtil.getIssueCompanyNo(webInput);
		String sql = "select nvl(count(*),0) from VoucherAdjustOrder where "
				+ "IssueCompanyNo='"
				+ issuecompanyno
				+ "' "
				+ "and (?1 is null or TranTime >=to_date(?1,'yyyy-mm-dd hh24:mi:ss')) "
				+ "and (?2 is null or TranTime <=to_date(?2,'yyyy-mm-dd hh24:mi:ss')) ";
		sql += PCPSUtil.getSqlCond(webInput, BranchNo, "BranchNo");
		if (!PCPSUtil.isEmpty(status)) {
			sql = sql + " and status = '" + status + "'";
		}
		if (!PCPSUtil.isEmpty(TranType)) {
			sql = sql + " and TranType = '" + TranType + "'";
		}
		sql = sql + " and TranType not in('11','12','13')";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, startTranTime);
		query.setParameter(2, endTranTime);
		return new BigInteger(query.getSingleResult().toString());
	}

	/**
	 * 根据条件查询凭证调整单列表 --调整单取消申请查询
	 * 
	 * @param BranchNo
	 *            调入机构号
	 * @param status
	 *            状态
	 * @param tranType
	 *            交易类型
	 * @param startTranTime
	 *            起始时间
	 * @param endTranTime
	 *            终止时间
	 * @param page
	 *            页码
	 * @return 凭证调整单列表
	 */
	@SuppressWarnings("unchecked")
	public List<VoucherAdjustOrder> getVoucherAdjustOrderByCancelList(
			PCPSWebInput webInput, String BranchNo, String status,
			String TranType, String startTranTime, String endTranTime,
			Integer page) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo())
				|| PCPSUtil.isEmpty(startTranTime)
				|| PCPSUtil.isEmpty(endTranTime) || PCPSUtil.isEmpty(BranchNo))
			return null;
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String issuecompanyno = PCPSUtil.getIssueCompanyNo(webInput);
		String sql = "select * from VoucherAdjustOrder where IssueCompanyNo='"
				+ issuecompanyno + "' and TranTime >=to_date('" + startTranTime
				+ "','yyyy-mm-dd hh24:mi:ss') and TranTime <=to_date('"
				+ endTranTime + "','yyyy-mm-dd hh24:mi:ss')";

		sql += PCPSUtil.getSqlCond(webInput, BranchNo, "BranchNo");

		if (!PCPSUtil.isEmpty(status)) {
			sql = sql + " and status = '" + status + "'";
		}
		if (!PCPSUtil.isEmpty(TranType)) {
			sql = sql + " and TranType = '" + TranType + "'";
		}
		sql = sql + " and TranType not in('01','02','03')";
		sql = sql + " order by TranTime desc ";
		Query query = _em.createNativeQuery(sql, VoucherAdjustOrder.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		return query.getResultList();
	}

	/**
	 * @param BranchNo
	 *            --调整单取消申请查询 调入机构号
	 * @param status
	 *            状态
	 * @param tranType
	 *            交易类型
	 * @param startTranTime
	 *            起始时间
	 * @param endTranTime
	 *            终止时间
	 * @return 凭证调整单录数
	 */
	@Override
	public BigInteger getVoucherAdjustOrderByCancelListTotal(
			PCPSWebInput webInput, String BranchNo, String status,
			String TranType, String startTranTime, String endTranTime) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo())
				|| PCPSUtil.isEmpty(BranchNo))
			return BigInteger.ZERO;
		String issuecompanyno = PCPSUtil.getIssueCompanyNo(webInput);
		String sql = "select nvl(count(*),0) from VoucherAdjustOrder where "
				+ "IssueCompanyNo='"
				+ issuecompanyno
				+ "' "
				+ "and (?1 is null or TranTime >=to_date(?1,'yyyy-mm-dd hh24:mi:ss')) "
				+ "and (?2 is null or TranTime <=to_date(?2,'yyyy-mm-dd hh24:mi:ss')) ";
		sql += PCPSUtil.getSqlCond(webInput, BranchNo, "BranchNo");

		if (!PCPSUtil.isEmpty(status)) {
			sql = sql + " and status = '" + status + "'";
		}
		if (!PCPSUtil.isEmpty(TranType)) {
			sql = sql + " and TranType = '" + TranType + "'";
		}
		sql = sql + " and TranType not in('01','02','03')";
		Query query = _em.createNativeQuery(sql);
		if (PCPSUtil.isEmpty(startTranTime)) {
			query.setParameter(1, "");
		} else {
			query.setParameter(1, startTranTime);
		}
		if (PCPSUtil.isEmpty(endTranTime)) {
			query.setParameter(2, "");
		} else {
			query.setParameter(2, endTranTime);
		}
		return new BigInteger(query.getSingleResult().toString());
	}
}
